<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Rules;

use App\Contacts\EnumInterface;
use Illuminate\Contracts\Validation\Rule;

class CheckEnum implements Rule
{
    /**
     * @var EnumInterface;
     */
    protected $enum;

    /** @var string */
    private $message;

    /**
     * Create a new rule instance.
     */
    public function __construct(EnumInterface $enum)
    {
        $this->enum = $enum;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param string $attribute
     * @param mixed $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (! isset($this->enum::toArray()[$value])) {
            $this->message = sprintf('The %s params invalid', $attribute);
            return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->message;
    }
}
